package set;

import java.util.Set;
import java.util.TreeSet;

/**
 * Created by SliverZhang on 2017/11/19.
 */
public class Book implements Comparable<Book> {
    private String title;
    private double price;

    public Book(String title, double price) {
        this.title = title;
        this.price = price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (Double.compare(book.price, price) != 0) return false;
        return title != null ? title.equals(book.title) : book.title == null;

    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = title != null ? title.hashCode() : 0;
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }
    /*    @Override
    public boolean equals(Object obj) {
        if (this==obj){
            return true;
        }
        if (obj == null){
            return false;
        }
        if (!(obj instanceof list.Book)){
            return false;
        }
        Book book = (Book) obj;
        if (this.title.equals(book.title) && this.price==book.price){
            return true;
        }
        return false;
    }*/

    @Override
    public String toString() {
        return "Book{" +
                "title='" + title + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public int compareTo(Book o) {
        if (this.price > o.price){
            return 1;
        }else if (this.price < o.price){
            return -1;
        }else {
            return this.title.compareTo(o.title);
        }
    }
}
class TestDemo{
    public static void main(String[] args) {
        Set<Book> all = new TreeSet<>();
        all.add(new Book("Java开发",79.8));
        all.add(new Book("Java开发",79.8));
        all.add(new Book("JSP开发",79.8));
        all.add(new Book("Android开发",89.8));
        System.out.println(all);
    }
}


















